﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Diagnostics;
using MemcachedDemo.Library.Client;
using LambdaDemo.ConsoleApp.Models;
using LambdaDemo.ConsoleApp.SerializeDemo;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data.Linq.SqlClient;

namespace LambdaDemo.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var db = new NorthwindDataContext();

            var q =
    from e in db.Employees
    where e.HomePhone.Substring(4, 1) == ")"
    select e.HomePhone.Remove(9);

            ObjectDumper.Write(q);

            //SerializeUtility su = new SerializeUtility();
            //su.XMLSerialize();

            Console.Read();
        }

        private static void ShowWindowsMessage(string message)
        {
            //MessageBox.Show(message);
        }

        static void GetListValue(List<string> list)
        {
            Console.WriteLine(string.Join(",", list.ToArray()));
        }

        public static bool isValidProduct(Products p)
        {
            if (p.ProductName.Length > 25)
            {
                return true;
            }
            return false;
        }

        static void TestLinqAttachUpdate()
        {
            #region
            //通常，通过从其他层反序列化 XML 来获取要附加的实体
            //不支持将实体从一个DataContext附加到另一个DataContext
            //因此若要复制反序列化实体的操作，将在此处重新创建这些实体
            Customers c1;
            List<Orders> deserializedOrders = new List<Orders>();
            Customers deserializedC1;
            using (NorthwindDataContext tempdb = new NorthwindDataContext())
            {
                c1 = tempdb.Customers.Single(c => c.CustomerID == "ALFKI");

                deserializedC1 = new Customers
                {
                    Address = c1.Address,
                    City = c1.City,
                    CompanyName = c1.CompanyName,
                    ContactName = c1.ContactName,
                    ContactTitle = c1.ContactTitle,
                    Country = c1.Country,
                    CustomerID = c1.CustomerID,
                    Fax = c1.Fax,
                    Phone = c1.Phone,
                    PostalCode = c1.PostalCode,
                    Region = c1.Region
                };
                Customers tempcust =
                    tempdb.Customers.Single(c => c.CustomerID == "ANTON");
                foreach (Orders o in tempcust.Orders)
                {
                    deserializedOrders.Add(new Orders
                    {
                        CustomerID = o.CustomerID,
                        EmployeeID = o.EmployeeID,
                        Freight = o.Freight,
                        OrderDate = o.OrderDate,
                        OrderID = o.OrderID,
                        RequiredDate = o.RequiredDate,
                        ShipAddress = o.ShipAddress,
                        ShipCity = o.ShipCity,
                        ShipName = o.ShipName,
                        ShipCountry = o.ShipCountry,
                        ShippedDate = o.ShippedDate,
                        ShipPostalCode = o.ShipPostalCode,
                        ShipRegion = o.ShipRegion,
                        ShipVia = o.ShipVia
                    });
                }
            }
            using (NorthwindDataContext db2 = new NorthwindDataContext())
            {
                //将第一个实体附加到当前数据上下文，以跟踪更改
                //对Customer更新，不能写错
                db2.Customers.Attach(deserializedC1);
                //更改所跟踪的实体
                deserializedC1.Address = "123 First Ave";
                //附加订单列表中的所有实体
                db2.Orders.AttachAll(deserializedOrders);
                //将订单更新为属于其他客户
                foreach (Orders o in deserializedOrders)
                {
                    o.CustomerID = "CHOPS";
                }
                //在当前数据上下文中提交更改
                db2.SubmitChanges();
            }
            #endregion
        }

        static void ReadCacheValue()
        {
            #region
            Stopwatch timer = new Stopwatch();
            var count = 0;
            for (var i = 1; i <= 100; i++)
            {
                timer.Restart();
                for (var k = 0; k < 100; k++)
                {
                    var value = ((Dictionary<int, ShareCodeEntity>)MemcachedManager.Instance.Get(((int)DataClassEnum.Category).ToString())).Count;
                    var value1 = ((Dictionary<int, ShareCodeEntity>)MemcachedManager.Instance.Get(((int)DataClassEnum.Industry).ToString())).Count;
                    var value2 = ((Dictionary<int, ShareCodeEntity>)MemcachedManager.Instance.Get(((int)DataClassEnum.Location).ToString())).Count;
                    count = value + value1 + value2;
                }
                timer.Stop();
                Console.WriteLine(string.Concat(i, "--List--Read 3--Record:", count, "--", timer.ElapsedMilliseconds / 1000));

                timer.Restart();
                for (var k = 0; k < 100; k++)
                {
                    var value = (UserInfo)MemcachedManager.Instance.Get("UserInfo");
                }
                timer.Stop();
                Console.WriteLine(string.Concat(i, "--Single Object--", timer.ElapsedMilliseconds / 1000));
            }
            #endregion
        }

    }
    public class NewCreateDB : DataContext
    {
        #region
        public Table<Person> Persons;
        public NewCreateDB(string connection)
            :
            base(connection)
        {
        }
        public NewCreateDB(System.Data.IDbConnection connection)
            :
            base(connection)
        {
        }
        #endregion
    }

    [Table(Name = "Person")]
    public partial class Person : INotifyPropertyChanged
    {
        #region
        private int _PersonID;
        private string _PersonName;
        private System.Nullable<int> _Age;

        public Person() { }

        [Column(Storage = "_PersonID", DbType = "INT",
            IsPrimaryKey = true)]
        public int PersonID
        {
            get { return this._PersonID; }
            set
            {
                if ((this._PersonID != value))
                {
                    this.OnPropertyChanged("PersonID");
                    this._PersonID = value;
                    this.OnPropertyChanged("PersonID");
                }
            }
        }

        [Column(Storage = "_PersonName", DbType = "NVarChar(30)")]
        public string PersonName
        {
            get { return this._PersonName; }
            set
            {
                if ((this._PersonName != value))
                {
                    this.OnPropertyChanged("PersonName");
                    this._PersonName = value;
                    this.OnPropertyChanged("PersonName");
                }
            }
        }

        [Column(Storage = "_Age", DbType = "INT")]
        public System.Nullable<int> Age
        {
            get { return this._Age; }
            set
            {
                if ((this._Age != value))
                {
                    this.OnPropertyChanged("Age");
                    this._Age = value;
                    this.OnPropertyChanged("Age");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string PropertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this,
                    new PropertyChangedEventArgs(PropertyName));
            }
        }

        #endregion
    }


}
